package com.tongzhan.controller;

import com.tongzhan.base.BaseController;
import com.tongzhan.base.Return;
import com.tongzhan.entity.party.NewPerson;
import com.tongzhan.param.NewPersonParam;
import com.tongzhan.utils.SnowFlakeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 发展新员
 *
 * @author htmic
 * @date 2020/7/29
 */
@RequestMapping("/person")
@RestController
public class NewPersonController extends BaseController {

    /**
     * 列表
     *
     * @param page
     * @param size
     * @param type           是否批示  0不是  1是
     * @param userId         用户id
     * @param year_and_month YYYY-MM 如果是全年：YYYY
     * @return
     */
    @PostMapping("/newPersonList")
    public Return list(Integer page, Integer size, Integer type, String userId, String year_and_month) {
        page = page == null ? 0 : page - 1;
        List list = null;
        list = newPersonService.getList(type, page, size, userId, year_and_month);
        return Return.getSuccessResult(list);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @PostMapping("/newPersonDetail")
    public Return getDetail(String id, String userId) {
        if (StringUtils.isEmpty(id)) {
            return Return.getErrorResult("缺少id参数!");
        }
        NewPerson detail = newPersonService.getDetail(id, userId);
        return Return.getSuccessResult(detail);
    }

    /**
     * 添加
     *
     * @param saveModel
     * @return
     */
    @PostMapping("/addNewPerson")
    public Return add(NewPersonParam saveModel) {
        if (saveModel == null) {
            return Return.getErrorResult("参数为空");
        }
        if (saveModel.getName().length() > 20) {
            return Return.getErrorResult("名称过长");
        }
        if (saveModel.getPartyTypeId() == null) {
            return Return.getErrorResult("党派类型不能为空");
        }
        if (StringUtils.isEmpty(saveModel.getStart_picture())) {
            return Return.getErrorResult("请上传图片");
        }
        if (StringUtils.isEmpty(saveModel.getSecond_picture())) {
            return Return.getErrorResult("请上传图片");
        }
        if (StringUtils.isEmpty(saveModel.getEnd_picture())) {
            return Return.getErrorResult("请上传图片");
        }

        NewPerson entity = new NewPerson();
        BeanUtils.copyProperties(saveModel, entity);
        entity.setId(new SnowFlakeUtils(3, 3).nextId() + "");
        entity.setAdd_userid(saveModel.getUserId());
        entity.setGender(saveModel.getGender());
        entity.setTelephone(saveModel.getPhone());
        long l = newPersonService.add(entity);
        if (l > 0) {
            return Return.getSuccessResult("新增成功!");
        }
        return Return.getErrorResult("新增失败!");
    }

    /**
     * 审核新员
     *
     * @param id
     * @param type   0是不同意 1是同意
     * @param userId 审核人id
     * @return
     */
    @PostMapping("/auditNewPerson")
    public Return auditNewPerson(String id, Integer type, String userId) {
        if (StringUtils.isEmpty(id) || type == null) {
            return Return.getErrorResult("参数为空");
        }
        long flag = newPersonService.auditNewPerson(id, type, userId);
        if (flag > 0) {
            return Return.getSuccessResult("审核成功!");
        }
        return Return.getErrorResult("审核失败!");
    }
}
